﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.TradeChannel.Gyb.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb
{
    /// <summary>
    /// 转换帮助
    /// </summary>
    static class PayConvertUtils
    {
        /// <summary>
        /// 转为付款结果
        /// </summary>
        /// <param name="payResult"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public static ITradeOrderQueryResult ToPayResult(ITradePayResult payResult, ITradeChannel channel)
        {
            if (string.IsNullOrWhiteSpace(payResult.TermSsn))
            {
                channel.ThrowChannelValidationException("贵阳银行商户订单号(TermSsn)为null或空白。");
            }
            if (string.IsNullOrWhiteSpace(payResult.AcqSsn))
            {
                channel.ThrowChannelValidationException("贵阳银行网关流水号(AcqSsn)为null或空白。");
            }
            if (string.IsNullOrWhiteSpace(payResult.SettDate))
            {
                channel.ThrowChannelValidationException("贵阳银行清算日期(SettDate)为null或空白。");
            }
            var settDate = payResult.SettDate.Trim();
            if (settDate.Length != 8)
            {
                channel.ThrowChannelValidationException("贵阳银行清算日期(SettDate)格式不正确，不是yyyyMMdd的格式。");
            }
            if (payResult.TranAmt <= 0m)
            {
                channel.ThrowChannelValidationException("贵阳银行交易金额(TranAmt)小于或等于零。");
            }
            DateTime payDate;
            try
            {
                payDate = new DateTime(int.Parse(settDate.Substring(0, 4)),
                       int.Parse(settDate.Substring(4, 2)),
                       int.Parse(settDate.Substring(6, 2)));
            }
            catch (Exception)
            {
                throw channel.ThrowChannelValidationException("贵阳银行清算日期(SettDate)格式不正确，不是yyyyMMdd的格式。");
            }
            return new TradeOrderQueryResult(channel)
            {
                OrderNo = payResult.TermSsn,
                PayTime = payDate,
                TradeNo = payResult.AcqSsn,
                OrderMoney = payResult.TranAmt,
                Subject = "未知",
                Body = "未知",
                ExtraParam = "",
                OtherPartyAccount = "",
                OtherPartyAccountId = "",
                PayFee = 0m,
                RefundFee = 0m,
                RefundMoney = 0m
            };
        }
    }
}
